package cn.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: mayuanbao
 * @Date: 2018-11-26 11:38
 * @Description:
 */
@Controller
@RequestMapping(value = "/up")
public class upController {
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public void up(HttpServletRequest req, @RequestParam("f1")MultipartFile file,@RequestParam("desc")String desc) throws IOException {
        String realPath = req.getServletContext().getRealPath("/files");
        System.out.println("desc is " + desc + ", realPath is " + realPath);
        String name = file.getOriginalFilename();
        if (name != null) {
            System.out.println("name is: " + name);
//            name = name.substring(name.lastIndexOf("\\" ));
//            System.out.println("name is: " + name);
            InputStream in = file.getInputStream();
            FileCopyUtils.copy(in, new FileOutputStream(new File(realPath, name)));
            in.close();
        }
    }
}
